package com.gxa.modules.service.impl;

import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.CommissionDTO;
import com.gxa.modules.dto.DataStatisticsDTO;
import com.gxa.modules.dto.DetailedDTO;
import com.gxa.modules.mapper.DataStatisticsMapper;
import com.gxa.modules.service.DataStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class DataStatisticsServiceImpl implements DataStatisticsService {
    @Autowired
    private DataStatisticsMapper dataStatisticsMapper;
    @Override
    public RespResult overview() {
        List<Integer[]> list=new ArrayList<>();
        Integer[] i24=dataStatisticsMapper.findByDay();
        Integer[] i07=dataStatisticsMapper.findByWeek();
        Integer[] i30=dataStatisticsMapper.findByMonth();
        Integer[] count=dataStatisticsMapper.findAllCount();
//            new DataStatisticsDTO("客户管理","联系人","跟单管理","订单管理","合同管理","售后管理");
        list.add(i24);
        list.add(i07);
        list.add(i30);
        list.add(count);
        return RespResult.success(list);
    }

    @Override
    public RespResult analysis(String dateF, String dateL) {
        Map<String,List> map=new HashMap<>();
        //客户类型统计
        List<DataStatisticsDTO> list1=dataStatisticsMapper.analysis1(dateF,dateL);
//        客户级别统计
        List<DataStatisticsDTO> list2=dataStatisticsMapper.analysis2(dateF,dateL);
//        客户来源统计
        List<DataStatisticsDTO> list3=dataStatisticsMapper.analysis3(dateF,dateL);
//        所属行业
        List<DataStatisticsDTO> list4=dataStatisticsMapper.analysis4(dateF,dateL);
//        客户省份分布
        List<DataStatisticsDTO> list5=dataStatisticsMapper.analysis5(dateF,dateL);
//        客户产品
        List<DataStatisticsDTO> list6=dataStatisticsMapper.analysis6(dateF,dateL);
        map.put("khlx",list1);
        map.put("kutj",list2);
        map.put("khly",list3);
        map.put("sshy",list4);
        map.put("kusf",list5);
        map.put("kucp",list6);
        return RespResult.success(map);
    }

    @Override
    public RespResult statisticsByEmp() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.statisticsByEmpByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.statisticsByEmpByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.statisticsByEmp(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult statisticsByDept() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.statisticsByDeptByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.statisticsByDeptByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.statisticsByDept(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult orderStatisticsByEmp() {

        List<DataStatisticsDTO> week=dataStatisticsMapper.orderStatisticsByEmpByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.orderStatisticsByEmpByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.orderStatisticsByEmp(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult orderStatisticsByDept() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.orderStatisticsByDeptByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.orderStatisticsByDeptByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.orderStatisticsByDeptByDept(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult aStatisticsByEmp() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.aStatisticsByEmpByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.aStatisticsByEmpByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.aStatisticsByEmpByEmp(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult aStatisticsByDept() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.aStatisticsByDeptByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.aStatisticsByDeptByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.aStatisticsByDeptByDept(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult afterStatisticsByEmp() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.afterStatisticsByEmpByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.afterStatisticsByEmpByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.afterStatisticsByEmpByEmp(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult afterStatisticsByDept() {
        List<DataStatisticsDTO> week=dataStatisticsMapper.afterStatisticsByDeptByWeek();
        List<DataStatisticsDTO> month=dataStatisticsMapper.afterStatisticsByDeptByMonth();
        List<DataStatisticsDTO> all=dataStatisticsMapper.afterStatisticsByDeptByDept(null,null,null,null);
        Map map =new HashMap();
        map.put("week",week);
        map.put("month",month);
        map.put("all",all);
        return RespResult.success(map);
    }

    @Override
    public RespResult annualCurve() {
//        新增客户
        List<DataStatisticsDTO> xzkh=dataStatisticsMapper.annualCurve1();
//        跟单次数
        List<DataStatisticsDTO> gdcs=dataStatisticsMapper.annualCurve2();
//        订单数量
        List<DataStatisticsDTO> ddsl=dataStatisticsMapper.annualCurve3();
//        合同数量
        List<DataStatisticsDTO> htsl=dataStatisticsMapper.annualCurve4();
//        售后次数
        List<DataStatisticsDTO> shcs=dataStatisticsMapper.annualCurve5();
        Map map =new HashMap();
        map.put("xzkh",xzkh);
        map.put("gdcs",gdcs);
        map.put("ddsl",ddsl);
        map.put("htsl",htsl);
        map.put("shcs",shcs);
        return RespResult.success(map);
    }

    @Override
    public RespResult detailedByEmp(String dataF, String dataL,Integer currentPage, Integer pageSize) {
        List<DetailedDTO> detailedDTOS=dataStatisticsMapper.detailedByEmp(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> gdNumList=dataStatisticsMapper.statisticsByEmp(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> DdNumList=dataStatisticsMapper.orderStatisticsByEmp(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> HTNumList=dataStatisticsMapper.aStatisticsByEmpByEmp(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> ShNumList=dataStatisticsMapper.afterStatisticsByEmpByEmp(dataF,dataL,currentPage,pageSize);
        for(int i=0;i<detailedDTOS.size();i++){
            detailedDTOS.get(i).setGdNum(gdNumList.get(i).getNum());
            detailedDTOS.get(i).setDdNum(DdNumList.get(i).getNum());
            detailedDTOS.get(i).setHtNum(HTNumList.get(i).getNum());
            detailedDTOS.get(i).setShNum(ShNumList.get(i).getNum());
            detailedDTOS.get(i).setAddNum(null);
            detailedDTOS.get(i).setUpdateNum(null);
            detailedDTOS.get(i).setDelNum(null);

        }
        return null;
    }

    @Override
    public RespResult detailedByDept(String dataF, String dataL,Integer currentPage, Integer pageSize) {
        List<DetailedDTO> detailedDTOS=dataStatisticsMapper.detailedByDept(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> gdNumList=dataStatisticsMapper.statisticsByDept(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> DdNumList=dataStatisticsMapper.orderStatisticsByDeptByDept(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> HTNumList=dataStatisticsMapper.aStatisticsByDeptByDept(dataF,dataL,currentPage,pageSize);
        List<DataStatisticsDTO> ShNumList=dataStatisticsMapper.afterStatisticsByDeptByDept(dataF,dataL,currentPage,pageSize);
        for(int i=0;i<detailedDTOS.size();i++){
            detailedDTOS.get(i).setGdNum(gdNumList.get(i).getNum());
            detailedDTOS.get(i).setDdNum(DdNumList.get(i).getNum());
            detailedDTOS.get(i).setHtNum(HTNumList.get(i).getNum());
            detailedDTOS.get(i).setShNum(ShNumList.get(i).getNum());
            detailedDTOS.get(i).setAddNum(null);
            detailedDTOS.get(i).setUpdateNum(null);
            detailedDTOS.get(i).setDelNum(null);

        }
        return null;
    }

    @Override
    public RespResult commission(String dataF, String dataL, Integer currentPage, Integer pageSize) {

        List<CommissionDTO> commissionDTOS=dataStatisticsMapper.commission(dataF,dataL,currentPage,pageSize);
        List<Integer> totalMoney=dataStatisticsMapper.getTotalMoney(dataF,dataL,currentPage,pageSize);
        for(int i=0;i<commissionDTOS.size();i++){
            commissionDTOS.get(i).setTotalMoeny(totalMoney.get(i));
            commissionDTOS.get(i).setPercentage(null);
            commissionDTOS.get(i).setCommission(null);
        }

        return RespResult.success(commissionDTOS);
    }
}
